package com.czl.module_work.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.configure.PickerOptions
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.BuildingtBean
import com.czl.base.data.bean.DeviceTypeBean
import com.czl.base.data.bean.FloorBean
import com.czl.base.data.bean.TakeCareRecordBean
import com.czl.base.event.LiveBusCenter
import com.czl.module_work.BR
import com.czl.module_work.R
import com.czl.module_work.adapter.TakeCareRecordAdapter
import com.czl.module_work.databinding.WorkFragmentTakeCareRecordBinding
import com.czl.module_work.viewModel.MyOrderDetailViewModel
import com.czl.module_work.viewModel.TakeCareRecordViewModel
import com.lxj.xpopup.util.XPopupUtils


/**
 * 创建日期：2021/12/27  15:27
 * 类说明:
 * @author：86152
 */
@Route(path = AppConstants.Router.Work.F_WORK_TAKE_CARE_RECORD)
class TakeCareRecordFragment :
    BaseFragment<WorkFragmentTakeCareRecordBinding, TakeCareRecordViewModel>() {
    private lateinit var mAdapter: TakeCareRecordAdapter

    override fun useBaseLayout(): Boolean = false;


    override fun initContentView(): Int = R.layout.work_fragment_take_care_record

    override fun initVariableId(): Int = BR.viewModel

    override fun initData() {
        viewModel.tvTitle.set("保养记录")
        initAdapter()
        viewModel.refresh()
    }

    override fun initViewObservable() {
        LiveBusCenter.observeScanResultEvent(this, {
            viewModel.deviceNoText.set(it.msg)
        })
        viewModel.uc.clearOrderEvent.observe(this, {
            mAdapter.setNewInstance(mutableListOf())
        })
        viewModel.uc.loadOrderEvent.observe(this, { data ->
            binding.smartCommon.finishRefresh(true)
            binding.smartCommon.finishLoadMore()
            if(data.isNullOrEmpty())
                return@observe
            mAdapter.addData(data)
        })
        viewModel.uc.buildingFilterEvent.observe(this, {
            val optionsPick =
                OptionsPickerBuilder(
                    requireContext()
                ) { options1, options2, options3, v ->
                    binding.tvBuildingName.setText(if (TextUtils.isEmpty(it[options1].buildingName)) "空白" else it[options1].buildingName)
                    viewModel.setBuildingFilter(it.get(options1).buildingId)
                }.build<BuildingtBean>()
            if(!it.isNullOrEmpty()) {
                optionsPick.setPicker(it)
                optionsPick.show()
            }
        })
        viewModel.uc.floorFilterEvent.observe(this, {
            val optionsPick =
                OptionsPickerBuilder(
                    requireContext()
                ) { options1, options2, options3, v ->
                    binding.tvFloorName.setText(if (TextUtils.isEmpty(it[options1].floorName)) "空白" else it[options1].floorName)
                    viewModel.setFloorFilter(it.get(options1).floorId)
                }.build<FloorBean>()
            optionsPick.setPicker(it)
            optionsPick.show()
        })
        viewModel.uc.deviceTypeFilterEvent.observe(this, {
            val optionsPick =
                OptionsPickerBuilder(
                    requireContext()
                ) { options1, options2, options3, v ->
                    binding.tvDeviceTypeName.setText(if (TextUtils.isEmpty(it[options1].pointTypeName)) "空白" else it[options1].pointTypeName)
                    viewModel.setDeviceTypeFilter(it.get(options1).pointTypeId)
                }.build<DeviceTypeBean>()
            optionsPick.setPicker(it)
            optionsPick.show()
        })
    }

    private fun initAdapter() {
        mAdapter = TakeCareRecordAdapter()
        binding.ryCommon.apply {
            adapter = mAdapter
        }
        val emptyView = View.inflate(context, R.layout.common_empty_layout_view, null)
        val titleView = emptyView.findViewById<TextView>(R.id.tv_empty)
        emptyView.findViewById<View>(R.id.ll_empty).setOnClickListener { v: View? -> binding.smartCommon.autoRefresh() }
        titleView.text = "该设备暂无保养工单"
        mAdapter.setEmptyView(emptyView)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            startContainerActivity(
                AppConstants.Router.Work.F_WORK_TAKE_CARE_ORDER_DETAIL,
                Bundle().apply {
                    putInt(
                        AppConstants.BundleKey.WORK_ORDER_STATUS,
                        MyOrderDetailViewModel.FILTER_ORDER_TYPE_WARNING
                    )
                    putInt(
                        AppConstants.BundleKey.WORK_ORDER_TYPE,
                        MyOrderDetailViewModel.FILTER_ORDER_TYPE_TAKE_CARE
                    )
                    putString(
                        AppConstants.BundleKey.WORK_ORDER_ID,
                        mAdapter.data[position].orderId
                    )
                })
        }
    }
}